var express = require('express');
var router = express.Router();
var fdmdao = require('../dao/fdmdao');
var utils = require('../common/utils');
var upload = require('../common/upload');
var config = require('../config');
var fileimport = require('../services/fileimport');

router.get('/manage', function (req, res, next) {
    res.render('fdm/list');
});


router.get('/upload', function (req, res, next) {
    res.render('fdm/upload');
});

router.get('/addfolder', function (req, res, next) {
    res.render('fdm/addfolder');
});

router.get('/resource', function (req, res, next) {
    res.render('fdm/resource');
});
router.get('/import', function (req, res, next) {
    res.render('fdm/import');
});
//上传
router.post('/upload', function (req, res, next) {
    upload.upLoadImage(req, res);
});
router.post('/uploadjson', function (req, res, next) {
    var folderid = req.headers.fid;
    var md5path = req.headers.md5path;
    var path = req.headers.path;
    upload.uploadjson(req, res).then(function (data) {
        fileimport.import(data, {
            md5path: md5path,
            folderid: folderid,
            path:path
        });
        return res.json({
            "code": 0,
            "msg": "操作成功，后台持续处理中"
        });
    }, function (err) {
        return res.json({
            "code": 1,
            "msg": "操作失败"
        });
    });
});
router.get('/list', function (req, res, next) {
    var page = req.query.page || 1; //页号
    var limit = req.query.limit || 10; //页大小
    var key = req.query.key || ''; //查询条件
    var condition = {
        page: parseInt(page),
        limit: parseInt(limit),
        key: key
    };
    fdmdao.getlist(condition).then(function (data) {
        res.json({
            "code": 0,
            "msg": "",
            count: data[1][0].total,
            data: data[0]
        });
    }, function (error) {
        res.json({
            "code": 1,
            "msg": error.message
        });
    })
});


router.post('/addfolder', function (req, res, next) {
    var foldername = req.body.foldername;
    var attr = req.body.attr || '';
    var folderid = utils.generalUUID();
    var path = config.rootdir + folderid + '\\';

    var params = {
        folderid: folderid,
        foldername: foldername,
        attr: attr,
        path: path.replace(/\\/g, '\\\\')
    };

    fdmdao.addfolder(params).then(function (data) {
        console.log(data);

        if (data) {
            return res.json({
                code: 1
            });
        } else {
            return res.json({
                code: 0
            });
        }
    });
});

router.post('/addresource', function (req, res, next) {
    var resourcename = req.body.resourcename;
    var folderid = req.body.folderid;
    var resourceid = utils.generalUUID();

    var params = {
        folderid: folderid,
        resourcename: resourcename,
        resourceid: resourceid
    };

    fdmdao.addresource(params).then(function (data) {
        console.log(data);

        if (data) {
            return res.json({
                code: 1
            });
        } else {
            return res.json({
                code: 0
            });
        }
    });
});

router.get('/resourcelist', function (req, res, next) {
    var page = req.query.page || 1; //页号
    var limit = req.query.limit || 10; //页大小
    var folderid = req.query.folderid || ''; //文件夹ID
    var condition = {
        page: parseInt(page),
        limit: parseInt(limit),
        folderid: folderid
    };
    fdmdao.resourcelist(condition).then(function (data) {
        return res.json({
            code: 0,
            msg: '',
            count: data[1][0].total,
            data: data[0]
        });
    }, function (error) {
        return res.json({
            code: 1,
            msg: error.message
        });
    });
});
module.exports = router;